using System;
using System.Collections.Generic;
using System.Text;
using OR.Reuse.Persistence;
using System.Drawing;

namespace OR.Lazarus.Business
{
	public class LogViewerManager<TYPE>
		: ILogManager
		where TYPE : ILogViewer
	{
		private List<TYPE> mItems;

		private Color mCompilerStateColor;
		private Color mErrorColor;
		private Color mGenerationColor;

		public LogViewerManager()
		{
			mItems = new List<TYPE>();
		}

		public Color GenerationColor
		{
			get { return mGenerationColor; }
			set { mGenerationColor = value; }
		}


		public Color ErrorColor
		{
			get { return mErrorColor; }
			set { mErrorColor = value; }
		}

		public Color CompilerStateColor
		{
			get { return mCompilerStateColor; }
			set { mCompilerStateColor = value; }
		}


		public List<TYPE> Items
		{
			get { return mItems; }
			set { mItems = value; }
		}

		#region ILogViewer Members

		public void AddCompileFileGenerated( string pCompiledFilename, long pCompiledFilesize )
		{
			string vMessage = String.Format( "\nSource transformed to Compiled file [ file://{0} ] ( {1:n0} bytes ).\n", DataConverter.FileUrlEncode( pCompiledFilename ), pCompiledFilesize );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddCompleteCompile( string pCompiledFilename, long pCompiledFilesize )
		{
			string vMessage = String.Format( "\nCompiled Source file to [ file://{0} ] ( {1:n0} bytes ).\n", DataConverter.FileUrlEncode( pCompiledFilename ), pCompiledFilesize );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddError( Exception pException )
		{
			string vMessage = String.Format( "\n** ERROR: {0}\n", pException.Message );

			foreach ( TYPE vViewer in mItems )
			{
				this.AddMessage( mErrorColor, vMessage );
			}
		}

		public void AddFileLoadLog( FileLoadEventArgs pArgs )
		{
			string vMessage = string.Format( "{0} File [ file://{1} ] ( {2:n0} bytes ) successfully loaded.", pArgs.FileType, DataConverter.FileUrlEncode( pArgs.FileName ), pArgs.FileSize );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mGenerationColor, vMessage );
			}
		}

		public void AddGenerateCompleted( GenerateCompleteEventArgs pArgs )
		{
			string vMessage = String.Format( "Finished compiling Source ( {0:n0} bytes ) into {1:n0} bytes of compile-ready source code.  This provides a conversion ratio {2:n2}:1", pArgs.SourceSize, pArgs.CompiledSize, pArgs.Ratio );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddGenerationLog( GenerateEventArgs pArgs )
		{
			string vMessage = String.Format( "File [ file://{0} ] ( {1:n0} bytes ) generated from transform [ file://{2} ].", DataConverter.FileUrlEncode( pArgs.Filename ), pArgs.FileSize, DataConverter.FileUrlEncode( pArgs.XslName ) );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mGenerationColor, vMessage );
			}
		}

		public void AddLineBreak()
		{
			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddLineBreak();
			}
		}

		public void AddMessage( System.Drawing.Color pColor, string pMessage )
		{
			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( pColor, pMessage );
			}
		}

		public void AddSourceCompilerCompleted( SourceCompileCompleteEventArgs pArgs )
		{
			string vMessage = String.Format( "Finished compiling Source ( {0:n0} bytes ) into Compiled ( {1:n0} bytes ) model.  * Conversion ratio {2:n2}:1", pArgs.SourceSize, pArgs.CompiledSize, pArgs.Ratio );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddSourceCompilerStarted( SourceCompileStartEventArgs pArgs )
		{
			string vMessage = String.Format( "Started compiling Source ( {0:n0} bytes ) into Compiled model.", pArgs.SourceSize );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddStartCompile( string pSourceFilename, long pSourceFilesize )
		{
			string vMessage = String.Format( "\nStarted Compilation of Source file [ file://{0} ] ( {1:n0} bytes ).\n", DataConverter.FileUrlEncode( pSourceFilename ), pSourceFilesize );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( mCompilerStateColor, vMessage );
			}
		}

		public void AddTierGenerationCompletedMessage( TierGenerateEventArgs pArgs )
		{
			string vMessage = String.Format( "Finished generating tier [ {0} ].", pArgs.Tier.Type );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( Color.Blue, vMessage );
			}
		}

		public void AddTierGenerationStartedMessage( TierGenerateEventArgs pArgs )
		{
			string vMessage = String.Format( "Started generating tier [ {0} ].", pArgs.Tier.Type );

			foreach ( TYPE vViewer in mItems )
			{
				vViewer.AddMessage( Color.Blue, vMessage );
			}
		}

		#endregion
	}
}
